<?php
/*
 * This file is part of phpep.
 * 
 * phpep
 * 
 * Copyright (C) 2007 Andrés Otárola Alvarado pgugged/cast -- otarola.andres@gmail.com
 * 
 * phpep is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * phpep is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */
session_start();
require_once 'AppTpl.class.php';

function __autoload($class_name){
	require_once(APP_DIR . "app/controllers/{$class_name}.class.php");
}

abstract class RootController {
	static private $_uncallable_actions = array("__construct","__destruct","route");
	protected $default_controller = "App";
	protected $tpl ;
	protected $session_actions = array();
	protected $session_redirect;
	protected $session_type;
	protected $route_params;
    function __construct() {
    	$this->tpl = new AppTpl(); 
    	$this->tpl->assign("logged_in",$_SESSION['sitweb_session_type']);
    	$this->route();
    	
    }
    protected function get_path_info(){
    	$action = $this->route_params['action'];
    	$controller = $this->route_params['controller'];
    	return VHOST_PREFIX . APP_NAME_PREFIX . "{$controller}/{$action}/";
    }
    
    protected function process_action(){
    	
    	$action = $this->route_params['action'];
    	$action = empty($action) ? 'index' : $action;
    	if(in_array($action,self::$_uncallable_actions))
    		throw new Exception("error!");
    	if(in_array($action,$this->session_actions)){
    		$session_type = $_SESSION['sitweb_session_type'];
    		if($session_type == $this->session_type){
    			$this->$action();
    		}
    		else if(isset($this->session_redirect['controller'])){
    			
    			$default_controller = $this->session_redirect['controller'];
				$default_action = $this->session_redirect['action'];
				$this->redirect_to($default_controller,$default_action);
    		}
    		else{
    			$this->redirect_to($this->default_controller);
    		}
    	}
    	else{
    		$this->$action();
    	}    	
    }    
    
    protected function redirect_to($controller, $action = 'index', $value = null){
    	/*$_SERVER['PATH_INFO'] = 'sitweb/'.$controller.'/'.$action;
    	$class = $this->get_class_controller_name($controller);
    	new $class();*/
    	header( "Location: ".VHOST_PREFIX.APP_NAME_PREFIX."{$controller}/{$action}/{$value}" );
    	exit(0); 
    }
    
    protected function flash($msg){
    	$_SESSION['flash_message'] = $msg;
    }
    
    protected function is_logged_in(){
    	return $this->session_type == $_SESSION['sitweb_session_type'];
    }

    public function is_postback(){
    	return (count($_POST) > 0 || isset($_POST['submit']));
    }        
   
    private function get_class_controller_name($name){
    	return ucwords($name).'Controller';
    }
    
    private function get_uri_params(){
    	$uri_array = explode('/' , $_SERVER['PATH_INFO']);
    	$tmp = array();
    	$tmp['controller'] = $uri_array[1];
    	$tmp['action'] = $uri_array[2];
    	$tmp['value'] = $uri_array[3];
    	return $tmp;
    }
    
    protected function route(){
    	$this->route_params = $this->get_uri_params();
    	if(get_class($this) == "AppController"){
    		    		
    		if(empty($this->route_params['controller'])){
    			$this->redirect_to($this->default_controller);
    		}
    		else{
    			$class = $this->get_class_controller_name($this->route_params['controller']);
    			new $class();
    		}
    			
    	}
    	else{
    		$this->process_action();
    	}
    }
    abstract protected function index();
}
?>